using UnityEngine;

namespace HybridCLR.Extension
{
    public class ResourcesStreamingAssets : IResources
    {
        public async System.Threading.Tasks.Task<T> LoadAssetAsync<T>(string path) where T : Object
        {
            if (typeof(T) != typeof(byte[]))
            {
                throw new System.Exception("Only load asset of type byte[]");
            }
            var request = UnityEngine.Networking.UnityWebRequest.Get(System.IO.Path.Combine(Application.streamingAssetsPath, path));
            System.Threading.Tasks.TaskCompletionSource<byte[]> tcs = new System.Threading.Tasks.TaskCompletionSource<byte[]>();
            request.SendWebRequest().completed += operation =>
            {
                if (request.result == UnityEngine.Networking.UnityWebRequest.Result.Success)
                {
                    tcs.SetResult(request.downloadHandler.data);
                }
                else
                {
                    tcs.SetResult(null);
                }
            };
            var bytes = await tcs.Task;
            return (T) (object) bytes;
        }

        public void Release<T>(T obj) where T : Object
        {
            
        }
    }
}